package com.tim.third.annotation;

import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.annotation.JSONType;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.google.gson.*;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.internal.bind.DateTypeAdapter;
import com.google.gson.internal.bind.JsonAdapterAnnotationTypeAdapterFactory;
import com.google.gson.internal.bind.SqlDateTypeAdapter;
import com.google.gson.internal.bind.TimeTypeAdapter;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Date: 2023/8/18 11:40
 *
 * @Author: Tim
 *
 * 全局定制序列化；注入对应 HttpMessageConverter
 */
public class RespSerialize {
    /**
     * https://github.com/alibaba/fastjson/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
     */
    class FastJson {
        public void fastjson() {
            JSONField jsonField;
            JSONType jsonType;

            // 项目定制序列化，注入bean
            FastJsonHttpMessageConverter converter;
            {
                SerializerFeature sf;// 格式枚举
            }
        }

        public void customize(){
//            @Bean
//            public HttpMessageConverters getFastJSONHttpMessageConvert() {
//                // 定义一个转换消息的对象
//                FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//                // 添加fastjson的配置信息 比如 ：是否要格式化返回的json数据
//                FastJsonConfig fastJsonConfig = new FastJsonConfig();
//
//                // 修改配置返回内容的过滤
//                fastJsonConfig.setSerializerFeatures(
//                        // 格式化输出
//                        SerializerFeature.PrettyFormat,
//                        // 消除循环引用
//                        SerializerFeature.DisableCircularReferenceDetect,
//                        // 返回结果保留null值
//                        SerializerFeature.WriteMapNullValue,
//                        // 将返回值为null的字符串转变成"",在这里可以自己设置
//                        SerializerFeature.WriteNullStringAsEmpty,
//                        // List字段如果为null,输出为[],而非null
//                        SerializerFeature.WriteNullListAsEmpty
//                );
//
//                // 解决 SerializerFeature.WriteNullStringAsEmpty 不生效问题
//                ValueFilter valueFilter = (object, name, value) -> {
//                    if (null == value){
//                        value = "";
//                    }
//                    return value;
//                };
//
//                // 设置全局日期格式
//                fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
//
//                // 注入过滤器
//                fastJsonConfig.setSerializeFilters(valueFilter);
//
//                // Long、BigDecimal 序列化时转 String
//                SerializeConfig serializeConfig = SerializeConfig.globalInstance;
//                serializeConfig.put(Long.class, ToStringSerializer.instance);
//                serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
//                serializeConfig.put(BigDecimal.class, ToStringSerializer.instance);
//                // 在转换器中添加配置信息
//                fastJsonConfig.setSerializeConfig(serializeConfig);
//                fastConverter.setDefaultCharset(StandardCharsets.UTF_8);
//                fastConverter.setFastJsonConfig(fastJsonConfig);
//
//                // 解决中文乱码问题，相当于在Controller上的@RequestMapping中加了个属性produces = "application/json"
//                List<MediaType> mediaTypeList = new ArrayList<>();
//                mediaTypeList.add(MediaType.APPLICATION_JSON);
//                fastConverter.setSupportedMediaTypes(mediaTypeList);
//
//                return new HttpMessageConverters(fastConverter);
//            }
        }
    }

    /**
     * https://github.com/FasterXML/jackson
     */
    class Jackson {

        public void jackson(){
            JsonFormat jsonFormat;// 定制序列化，常用于日期
            JsonIgnore ignore;

            JsonSerialize jackson;// 定制序列化；日期、数值转字符串等
            com.fasterxml.jackson.databind.JsonSerializer jsonSerializer;

            // 项目定制序列化，注册bean
            // 定制 ObjectMapper
            Jackson2ObjectMapperBuilderCustomizer customizer;
            LocalDateTimeSerializer s;

            WebMvcConfigurationSupport c1;
            WebMvcAutoConfiguration c2;
            WebMvcConfigurerAdapter c3;
        }

        public void customize(){
//            @Bean
//            public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
//                // Long转字符串；需要导入spring-web包
//                return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer())
//                        .serializerByType(Long.class, ToStringSerializer.instance)
//                    .serializerByType(Long.TYPE, ToStringSerializer.instance);
//            }
        }

    }

    class Gson {

        public void gson(){
            JsonAdapter gson;
            {
                JsonAdapterAnnotationTypeAdapterFactory adapterFactory;
                TypeAdapterFactory typeAdapterFactory;

                TypeAdapter typeAdapter;
                DateTypeAdapter dateTypeAdapter;
                TimeTypeAdapter timeTypeAdapter;
                SqlDateTypeAdapter sqlDateTypeAdapter;// 可参考，修改为自定义的格式

                JsonSerializer jsonSerializer;
                JsonDeserializer jsonDeserializer;
            }

//        GsonHttpMessageConverter converter;

        }

        public void customize(){
//            @Bean
//            public HttpMessageConverters httpMessageConverters() {
//                GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
//                Gson gson = new GsonBuilder()
//                        .create();
//                gsonHttpMessageConverter.setGson(gson);
//                HttpMessageConverter<?> converter = gsonHttpMessageConverter;
//                return new HttpMessageConverters(converter);
//            }
        }

    }

}
